package com.wxx;

import java.util.Arrays;

/**
 * @author: Jetwang
 * @date: 2022/3/18 18:42
 * @description: SelectionSort 选择排序
 * @version: 1.0
 */
public class SelectionSort {
    public static void selectionSort(Integer[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr, i, minIndex);
        }
    }

    /**
     * 提取数字交换方法
     * @param arr
     * @param i
     * @param j
     */
    private static void swap(Integer[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        Integer[] arr = {2, 4, 1, 7, 3, 8, 5};
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
